<?php

include_once "dao/timeattendanceDAO.php";
include_once "dao/timeattendance.php";
require_once "dao/configuration.php";

$requestType = $_GET['request'];



switch($requestType) {
	
	case "login" : if(($data = login($_POST['userid'], $_POST['password'])) !="") {
					$_SESSION['id'] = $data['id'];
					$_SESSION['name'] = $data['name'];
					$_SESSION['role'] = $data['role'];
					$_SESSION['department'] = $data['department'];
					redirect($_GET['return'], "pass");
				   }
				   else {
				   	redirect($_GET['return'],"fail");
				   }	
		
		break;
		
	case "logout" : session_destroy();
					redirect($_GET['return'],"pass");
		break;
		
	case "upload-file" : readCSVFile();
		break;
		
	case "view-records" :   $taRecords = viewStaffRecord ($_POST['staff'], $_POST['start_date'], $_POST['end_date']);						
							$_SESSION['taRecords'] = json_encode($taRecords);
							$_SESSION['start_date'] = $_POST['start_date'];
							$_SESSION['end_date'] = $_POST['end_date'];
							redirect($_GET['return'], "pass&staffId=".$_POST['staff']."&staff=".getStaffName($_POST['staff'])."&startDate=".$_POST['start_date']."&endDate=".$_POST['end_date']);
		break;
		
	case "update-record" : $taRecord = new timeattendance($_POST['id']);
	
						   $date = explode("-", trim($_POST['date']));
						   $taRecord->setDate(mktime(0, 0, 0, $date[1], $date[2], $date[0]));
						   
						   if(trim($_POST['start_time'])!="")
						   		$taRecord->insertTiming($_POST['start_time']);
						   if(trim($_POST['end_time'])!="")		
						   		$taRecord->insertTiming($_POST['end_time']);
						   if(trim($_POST['break_start'])!="")
						   		$taRecord->insertTiming($_POST['break_start']);
						   if(trim($_POST['break_end'])!="")
						   		$taRecord->insertTiming($_POST['break_end']);
						   $taRecord->compute();
						   $taRecord->setWorkStartTime($_POST['work_start_time']);
						   $taRecord->setDay($_POST['day']);
						   $taRecord->setApprovalStatus(1);
						   $taRecord->setRemarks($_POST['remarks']);
						   
						   				  
						   if(changesToTime($taRecord)){
						  		insertApproval($taRecord);
						   }
						   
						   	updateTARemarks($taRecord);
						   	
						   	$taRecords = viewStaffRecord ($taRecord->getId(), $_SESSION['start_date'], $_SESSION['end_date']);						
							$_SESSION['taRecords'] = json_encode($taRecords);
						   	
						    redirect($domain."/viewRecords.php", "pass&staffId=".$taRecord->getId()."&staff=".getStaffName($taRecord->getId())."&startDate=".$_SESSION['start_date']."&endDate=".$_SESSION['end_date']);			   
		break;
		
	case "approve-records" : foreach ($_POST['approvalList'] as $key => $value) {
							 
								 $data = explode("&", $value);
								 updateApproval($data[0], $data[1], $_SESSION['id'], time());
							 }
							
							redirect($_GET['return'],"pass");
		break;
		
	case "export-records" : $date = explode("-", $_POST['date']);
							$records = exportToExcel($_POST['staff'], firstOfMonth($date[1], $date[0]), lastOfMonth($date[1], $date[0]));
							redirect($_GET['return'],"pass&id=".$_POST['staff']);
		break;
		
	case "change-password" : $id = $_SESSION['id'];
					         $password = $_POST['new_password1'];
							 changeUserPassword($id, $password);
						     redirect($_GET['return'],"pass");
	
		break;
								
			
}

function redirect($url, $result) {
	?>
	<script type="text/javascript">window.top.location = "<?php echo $url."?result=".$result; ?>";</script>
	<?php 
}


function firstOfMonth($month, $year) {
	return strtotime(date($month).'/01/'.date($year).' 00:00:00');
}

function lastOfMonth($month, $year) {
	return strtotime('-1 second',strtotime('+1 month',strtotime(date($month).'/01/'.date($year).' 00:00:00')));
}


?>